/*
 * Copyright 2013 University of Padua, Italy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/**
	@author: Coccato Mattia
	@date: 2013-07-31
	@revision: 1.0
**/

package it.unipd.dei.bd.icineasti.servlet;

import it.unipd.dei.bd.icineasti.database.ElencaSediDatabase;
import it.unipd.dei.bd.icineasti.resource.Sede;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletOutputStream;
import javax.sql.DataSource;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

/** Servlet wrapper attorno alla generazione del report JasperReport per la stampa dei biglietti utente di uno spettacolo.
 */
public class PrintBigliettiServlet extends AbstractDatabaseServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {


		Connection con = null;

		try {
			con = DS.getConnection();
			
		} catch (NullPointerException e) {
			// imposta il tipo MIME della risposta
			res.setContentType("text/html");

			res.getWriter().printf("<p><center><font color=\"red\" size=\"+2\">Impossibile creare la connessione al database.</font></center></p>");
			return;

		} catch (SQLException e) {
			e.printStackTrace();
			// imposta il tipo MIME della risposta
			res.setContentType("text/html");
			res.getWriter().printf("<p><center><font color=\"red\" size=\"+2\">Errore durante l'accesso al database: %s</font></center></p>",
					e.getMessage());
			return;
		}

		//Costruisco la mappa dei parametri usati dal report
		HashMap<String, Object> parametri = new HashMap<String, Object>();
		
		//la popolo

		HttpSession session = req.getSession(true);

		try {
			parametri.put("UTENTE", session.getValue("id_utente"));  
			parametri.put("SPETTACOLO", Integer.parseInt(req.getParameter("spettacolo")));  
			parametri.put("IMAGE_BASE_DIR", getServletContext().getRealPath("WEB-INF"));
		} catch (NumberFormatException e) {
			e.printStackTrace();
			// imposta il tipo MIME della risposta
			res.setContentType("text/html");
			res.getWriter().printf("<p><center><font color=\"red\" size=\"+2\">Errore conversione parametro: %s</font></center></p>",
					e.getMessage());
			return;
		}
		
		
		String reportFileName = getServletContext().getRealPath("WEB-INF/biglietti.jasper");
		
		
		JasperPrint jasperPrint;

		ServletOutputStream outstream = res.getOutputStream();

		try {
			jasperPrint = JasperFillManager.fillReport(reportFileName, parametri, con);
			// imposta il tipo MIME della risposta
			res.setContentType("application/pdf");
			//res.addHeader("Content-Disposition", "attachment; filename=stampabiglietti.pdf");
			JasperExportManager.exportReportToPdfStream(jasperPrint, outstream );
		} catch (Exception ex) {
			// imposta il tipo MIME della risposta
			ex.printStackTrace();
			res.setContentType("text/html");
			outstream.print("<p><center><font color=\"red\" size=\"+2\">Errore durante la compilazione del report: " + ex.toString() + "</font></center></p>");

		}
		

		// assicura che tutto il contenuto sia inviato
		outstream.flush();

		// chiude il flusso di uscita
		outstream.close();

	}

}
